/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.UserCartPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.UserCartService;

@RestController
@RequestMapping("cart")
public class UserCartController extends GenericController {
	private static final long serialVersionUID = -7866967845414587L;
	public static final String CACHE_KEY = "userCart";
	@Autowired
	private UserCartService userCartService;
	
	/**
	 * 根据条件获取UserCartPo对象，并返回查询的数据总数
	 * 
	 * @param userCart
	 * @Author 迷蝶
	 * @return ResultPageTo<UserCartPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<UserCartPo> getUserCarts(@RequestBody UserCartPo userCart) {
        return userCartService.getUserCarts(userCart);
    }
	
	/**
	 * 根据ID获取UserCartPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserCartPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'userCart_'+#id")
	public UserCartPo getUserCart(@PathVariable("id") int id) {
		return userCartService.getUserCart(id);
	}
	
	/**
	 * 保存UserCartPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param userCart
	 * @Author 迷蝶
	 * @return UserCartPo
	 */
	@RequestMapping("save")
	@CacheEvict(value=CACHE_KEY, key="'userCart_'+#userCart.id")
	public Integer saveUserCart(@RequestBody UserCartPo userCart) {
		int rs = 0;
		if(null == userCart) {return rs;}
		if(null == userCart.getId()) {
			rs = userCartService.addUserCart(userCart);
		} else {
			rs = userCartService.updateUserCart(userCart);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除UserCartPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserCartPo
	 */
	@RequestMapping("del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'userCart_'+#id")
	public Integer delUserCart(@PathVariable("id") int id) {
		return userCartService.delUserCart(id);
	}
	
	/**
	 * 根据用户ID查找此用户购物车所有商品总数
	 * 
	 * @param userId
	 * @Author 迷蝶
	 * @return Integer
	 */
	@RequestMapping("wareSum/{userId:^[0-9]*$}")
	public Integer getWareSumByUserId(@PathVariable("userId") Integer uid) {
        return null == uid ? 0 : userCartService.getWareSumByUserId(uid);
	}
	
	/**
	 * 设置购物车商品数量，前提是购物车商品已经存在
	 * 
	 * @param userId		用户ID
	 * @param cartId	商品ID
	 * @param number	商品数量。设置之后的数量
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("setNumber/{userId:^[0-9]*$}/{cartId:^[0-9]*$}")
	public ResultTo setNumber(@PathVariable("userId")int userId, @PathVariable("cartId")int cartId, int number) {
		return userCartService.setNumber(userId, cartId, number);
	}
	
	/**
	 * 设置购物车商品数量，购物车商品已经存在则添加数量，不存在则新增
	 * 
	 * @param userId		用户ID
	 * @param username	用户名
	 * @param wareId	商品ID
	 * @param number	商品数量。设置之后的数量
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("addNumber/{userId:^[0-9]*$}/{wareId:^[0-9]*$}")
	public ResultTo addNumber(@PathVariable("userId")int userId, String username, 
			@PathVariable("wareId")int wareId, int number) {
		return userCartService.addNumber(userId, username, wareId, number);
	}
	
	/**
	 * 根据用户ID和购物车ID查找数据
	 * 
	 * @param userId	用户ID
	 * @param ids		购物车ID数组
	 * @Author 迷蝶
	 * @return List<UserCartPo>
	 */
	@RequestMapping("carts/{userId:^[0-9]*$}")
	public List<UserCartPo> getCartsByUserIdAndIds(@PathVariable("userId") Integer userId, Integer[] ids) {
		return userCartService.getCartsByUserIdAndIds(userId, ids);
	}
}